package com.jn.algorithm.day022.question69;

/**
 * @author 江南大学1033190417
 * @date 2022/5/14 0:17
 */
public class Solution {

    //二分搜索
    public int mySqrt(int x) {
        int l = 0, r = x, ans = -1;
        while (l <= r) {
            int mid = (l + r) / 2;
            if ((long) mid * mid <= x) {
                ans = mid;
                l = mid + 1;
            } else {
                r = mid - 1;
            }
        }
        return ans;
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        System.out.println(solution.mySqrt(9));
    }
}
